﻿using System;
using System.ServiceProcess;
using System.Configuration;
using Talentnet.Core.Utilities;
using Talentnet.Core.Utilities.Mail;

namespace TLN.WS
{
    partial class TalentnetService : ServiceBase
    {
        private log4net.ILog _logFirst = log4net.LogManager.GetLogger("FirstLogger");
        private log4net.ILog _logSecond = log4net.LogManager.GetLogger("SecondLogger");
        private object _lockFirstObj;
        private object _lockSecondObj;
        private double _firstThreadInterval;
        private double _secondThreadInterval;
        private System.Timers.Timer _firstThread;
        private System.Timers.Timer _secondThread;
        MailServiceSettings _mailServiceSettings;
        TalentnetBusiness _talentnetBusiness;

        public TalentnetService()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();

            double.TryParse(ConfigurationManager.AppSettings.Get("FirstThreadInterval"), out _firstThreadInterval);
            _firstThreadInterval = _firstThreadInterval * 1000;
            _firstThread = _firstThreadInterval > 0 ? new System.Timers.Timer(_firstThreadInterval) : new System.Timers.Timer();
            _firstThread.Elapsed += new System.Timers.ElapsedEventHandler(firstThread_Elapsed);
            
            double.TryParse(ConfigurationManager.AppSettings.Get("SecondThreadInterval"), out _secondThreadInterval);
            _secondThreadInterval = _secondThreadInterval * 1000;
            _secondThread = _secondThreadInterval > 0 ? new System.Timers.Timer(_secondThreadInterval) : new System.Timers.Timer();
            _secondThread.Elapsed += new System.Timers.ElapsedEventHandler(secondThread_Elapsed);
        }

#if DEBUG
        public
#else
        protected override
#endif
        void OnStart(string[] args)
        {
            base.OnStart(args);
            try
            {
                _lockFirstObj = new object();
                _lockSecondObj = new object();

                // Business class
                _talentnetBusiness = new TalentnetBusiness();

                // Init mail Settings
                _mailServiceSettings = new MailServiceSettings();             
                _mailServiceSettings.AuthenticationType = SmtpAuthentication.Basic;
                _mailServiceSettings.Server = ConfigurationManager.AppSettings.Get("SMTPServer");
                _mailServiceSettings.Username = ConfigurationManager.AppSettings.Get("SMTPUserName");
                _mailServiceSettings.Password = ConfigurationManager.AppSettings.Get("SMTPPassword");
                _mailServiceSettings.Port = Convert.ToInt32(ConfigurationManager.AppSettings.Get("SMTPPort"));
                //_mailServiceSettings.EnableSSL = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("SMTPSSL"));
            }
            catch (Exception ex)
            {
                _logFirst.Error("On start failed: " + ex.Message);
                ServiceController serviceController = new ServiceController(this.ServiceName);
                serviceController.Stop();
                return;
            }

            _logFirst.Info("******** Service Start ********");

#if DEBUG
            _firstThreadInterval = 0;
            _secondThreadInterval = 0;

            _firstThread.Interval = 10 * 1000;
            _secondThread.Interval = 10 * 1000;
            
            _firstThread.Start();
            _secondThread.Start();
#else          
            if (_firstThreadInterval > 0) _firstThread.Enabled = true;
            if (_secondThreadInterval > 0) _secondThread.Enabled = true;
#endif
        }

        protected override void OnStop()
        {
            _logFirst.Info("******** Service Stop ********");
            _firstThread.Enabled = false;
            _firstThread.Dispose();
            _secondThread.Enabled = false;
            _secondThread.Dispose();

            base.OnStop();
        }

        void firstThread_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (_lockFirstObj)
            {
                _talentnetBusiness.SendEmail(_mailServiceSettings);
                //_talentnetBusiness.SendEmailTest(_mailServiceSettings);               
            }
        }

        void secondThread_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (_lockSecondObj)
            {
                
            }
        }

    }
}
